from fastapi import HTTPException
from typing import Any

class AITechException(Exception):
    """基础异常类"""
    def __init__(self, message: str):
        self.message = message
        super().__init__(self.message)

class ValidationError(AITechException):
    """验证错误"""
    pass

class NotFoundError(AITechException):
    """未找到资源"""
    pass

class ServiceError(AITechException):
    """服务错误"""
    pass

class LLMError(AITechException):
    """LLM调用错误"""
    pass

def handle_exception(e: Exception) -> HTTPException:
    """异常处理器"""
    if isinstance(e, AITechException):
        if isinstance(e, NotFoundError):
            return HTTPException(status_code=404, detail=str(e))
        elif isinstance(e, ValidationError):
            return HTTPException(status_code=400, detail=str(e))
        elif isinstance(e, ServiceError):
            return HTTPException(status_code=500, detail=str(e))
        elif isinstance(e, LLMError):
            return HTTPException(status_code=503, detail=str(e))
    return HTTPException(status_code=500, detail="Internal server error") 